<template>
  <div class="playlist-container">
    <div class="top-wrap">
      <div class="img-wrap">
        <img :src="playlist.coverImgUrl" alt />
      </div>
      <div class="info-wrap">
        <p class="title">{{ playlist.name }}</p>
        <div class="author-wrap">
          <img class="avatar" :src="playlist.creator.avatarUrl" alt />
          <span class="name">{{ playlist.creator.nickname }}</span>
          <span class="time">{{ playlist.createTime }} 创建</span>
        </div>
        <div class="play-wrap" style="cursor: pointer" @click="playAll">
          <span class="iconfont icon-circle-play"></span>
          <span class="text">播放全部</span>
        </div>
        <div class="tag-wrap">
          <span class="title">标签:</span>
          <ul>
            <li v-for="(item, index) in playlist.tags" :key="index">
              {{ item }}
            </li>
          </ul>
        </div>
        <div class="desc-wrap">
          <span class="title">简介:</span>
          <span class="desc">{{ playlist.description }}</span>
        </div>
      </div>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲列表" name="1">
        <table class="el-table playlit-table">
          <thead>
            <th></th>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
          </thead>
          <tbody>
            <tr
              class="el-table__row"
              v-for="(item, index) in playlist.tracks"
              :key="index"
            >
              <td>{{ index + 1 }}</td>
              <td>
                <div class="img-wrap">
                  <img :src="item.al.picUrl" alt />
                  <span
                    class="iconfont icon-play"
                    @click="play(item.id)"
                  ></span>
                </div>
              </td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span>{{ item.name }}</span>
                    <span
                      class="iconfont icon-mv"
                      v-if="item.mv != 0"
                      @click="toMv(item.mv)"
                    ></span>
                  </div>
                  <span>{{ item.alia[0] }}</span>
                </div>
              </td>
              <td>{{ item.ar[0].name }}</td>
              <td>{{ item.al.name }}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane label="评论" name="2">
        <!-- 精彩评论 -->
        <div class="comment-wrap">
          <p class="title">
            精彩评论
            <span class="number">{{ hotComments.length }}</span>
          </p>
          <div class="comments-wrap">
            <div class="item" v-for="(item, index) in hotComments" :key="index">
              <div class="icon-wrap">
                <img :src="item.user.avatarUrl" alt />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{ item.user.nickname }}：</span>
                  <span class="comment">{{ item.content }}</span>
                </div>
                <div
                  class="re-content"
                  v-show="item.beReplied.length != 0"
                  v-for="(item1, index1) in item.beReplied"
                  :key="index1"
                >
                  <span class="name">{{ item1.user.nickname }}：</span>
                  <span class="comment">{{ item1.content }}</span>
                </div>
                <div class="date">{{ item.time }}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 最新评论 -->
        <div class="comment-wrap">
          <p class="title">
            最新评论
            <span class="number">({{ total }})</span>
          </p>
          <div class="comments-wrap">
            <div class="item" v-for="(item, index) in comments" :key="index">
              <div class="icon-wrap">
                <img :src="item.user.avatarUrl" alt />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{ item.user.nickname }}：</span>
                  <span class="comment">{{ item.content }}</span>
                </div>
                <div
                  class="re-content"
                  v-show="item.beReplied.length != 0"
                  v-for="(item2, index2) in item.beReplied"
                  :key="index2"
                >
                  <span class="name">{{ item2.user.nickname }}：</span>
                  <span class="comment">{{ item2.content }}</span>
                </div>
                <div class="date">{{ item.time }}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 分页器 -->
        <el-pagination
          @current-change="handleCurrentChange"
          background
          layout="prev, pager, next"
          :total="total"
          :current-page="page"
          :page-size="pageSize"
        ></el-pagination>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'playlist',
  data() {
    return {
      activeIndex: '1',
      // 总条数
      total: 0,
      // 页码
      page: 1,
      pageSize: 10,
      id: this.$route.query.id,
      playlist: {},
      hotComments: [],
      comments: [],
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.page = val
    },
    play(id) {
      axios({
        url: 'autumnfish/song/url',
        method: 'get',
        params: {
          id,
        },
      }).then((res) => {
        console.log(res)
        let url = res.data.data[0].url
        if (!url) {
          this.$message.error('无法播放这首歌，可能因为没有版权')
        }
        this.$parent.musicUrl = url
        this.$parent.volume = 0.2
      })
    },
    playAll() {
      this.$parent.playlist = this.playlist.tracks
      console.log(this.$parent.playlist)
    },
    toMv(id) {
      this.$router.push(`/mv?id=${id}`)
    },
    loadComments() {
      axios({
        url: '/autumnfish/comment/playlist',
        method: 'get',
        params: {
          id: this.id,
          limit: this.pageSize,
          offset: (this.page - 1) * this.pageSize,
        },
      }).then((res) => {
        if (this.page == 1) {
          this.hotComments = res.data.hotComments
        }
        this.comments = res.data.comments
        this.total = res.data.total
      })
    },
  },
  watch: {
    page() {
      this.loadComments()
    },
  },
  created() {
    axios({
      url: '/autumnfish/playlist/detail',
      method: 'get',
      params: {
        id: this.id,
      },
    }).then((res) => {
      this.playlist = res.data.playlist
    })

    this.loadComments()
  },
}
</script>

<style >
</style>
